﻿using Galaxy.AutoCAD.Databases;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using Autodesk.AutoCAD.Geometry;
using Galaxy.AutoCAD.Geometrys;
using System;
using Galaxy.AutoCAD.Editors;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsInterface;
using Galaxy.AutoCAD.Strings;
using Galaxy.AutoCAD.GraphicsInterface;
using Galaxy.AutoCAD.Hooks;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Text;
using System.Reflection;
using Autodesk.AutoCAD.Colors;

[assembly: CommandClass(typeof(Galaxy.AutoCAD.Methods.TestMethodTool))]
namespace Galaxy.AutoCAD.Methods
{
    public class TestMethodTool
    {
        [CommandMethod("WENZHANGONE")]
        public void WENZHANGONE()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Point3d pt1 = new Point3d(0, 0, 0);
            Point3d pt2 = new Point3d(2, 1, 0);
            Point3d pt3 = new Point3d(3, 2, 0);
            Point3d pt4 = new Point3d(4, 5, 0);
            Point3d pt5 = new Point3d(4, 6, 0);
            Point3d pt6 = new Point3d(3, 5, 0);
            List<Point3d> pt3ds = new List<Point3d>();
            pt3ds.Add(pt1);
            pt3ds.Add(pt2);
            pt3ds.Add(pt3);
            pt3ds.Add(pt4);
            pt3ds.Add(pt5);
            pt3ds.Add(pt6);
            int a;
            Point3d pt3d = Point3dTool.GetPoint3dOfMaximumX(pt3ds, out a);
            Application.ShowAlertDialog(pt3d.X + "\t\n" + pt3d.Y + "\t\n" + pt3d.Z + "\t\n" + a + "\t\n" + pt3ds.Count);
        }
        [CommandMethod("IsNumber")]
        public void IsNumber()
        {
            string a = "001";
            string b = "db001";
            string c = "db";
            string d = "-001";
            string e = "+001";
            string f = "!001";
            Application.ShowAlertDialog(StringTool.IsNumber(a).ToString());
            Application.ShowAlertDialog(StringTool.IsNumber(b).ToString());
            Application.ShowAlertDialog(StringTool.IsNumber(c).ToString());
            Application.ShowAlertDialog(StringTool.IsNumber(d).ToString());
            Application.ShowAlertDialog(StringTool.IsNumber(e).ToString());
            Application.ShowAlertDialog(StringTool.IsNumber(f).ToString());
        }
        bool veto = false;
        [CommandMethod("AddDoubleClickEvent")]
        public void AddDoubleClickEvent()
        {
            Application.DocumentManager.DocumentLockModeChanged += new DocumentLockModeChangedEventHandler(DoucumentManager_DocumentLockModeChanged);
            Application.BeginDoubleClick += new BeginDoubleClickEventHandler(Application_BeginDoubleClick);
        }
        void Application_BeginDoubleClick(object sender, BeginDoubleClickEventArgs e)
        {
            veto = true;
        }
        void DoucumentManager_DocumentLockModeChanged(object sender, DocumentLockModeChangedEventArgs e)
        {
            if (e.GlobalCommandName.ToLower() == "properties")
            {
                if (veto)
                {
                    e.Veto();
                    Document activeDoc = Application.DocumentManager.MdiActiveDocument;
                    PromptSelectionResult res = activeDoc.Editor.SelectImplied();
                    if (res.Value.Count != 1)
                    {
                        Application.ShowAlertDialog("请选中一个对象");
                        return;
                    }
                    ObjectId idSel = res.Value[0].ObjectId;
                    Application.ShowAlertDialog("你选中的对象ObjectId为：" + idSel.ToString());
                    veto = false;
                }
            }
        }
        [CommandMethod("RemoveDoubleClickEvent")]
        public void RemoveDoubleClickEvent()
        {
            Application.DocumentManager.DocumentLockModeChanged -= new DocumentLockModeChangedEventHandler(DoucumentManager_DocumentLockModeChanged);
            Application.BeginDoubleClick -= new BeginDoubleClickEventHandler(Application_BeginDoubleClick);
        }
        [CommandMethod("CeShiOne")]
        public static void CeShiOne()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Point3d choicePoint3d = InputTool.InputPoint(ed, "请选择点");
            DBObjectCollection island = ed.TraceBoundary(choicePoint3d, true);
            if (island.Count == 1)
            {
                DBObject dBObject = island[0];
                if (dBObject is Entity)
                {
                    Entity entity = dBObject as Entity;
                    ObjectId objectId = db.AddToModelSpace(entity);
                }
            }

        }
        private static CloudLineRule theCloudLineOverrule;
        [CommandMethod("CeShiTwo")]
        public static void CeShiTwo()
        {
            if (theCloudLineOverrule == null)
            {
                theCloudLineOverrule = new CloudLineRule();
                Overrule.AddOverrule(RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)), theCloudLineOverrule, true);
                theCloudLineOverrule.SetCustomFilter();
            }
            Overrule.Overruling = true;
            Application.DocumentManager.MdiActiveDocument.Editor.Regen();
        }
        private static LeadsLabelRule theLeadsLabelOverrule;
        [CommandMethod("CeShiThree")]
        public static void CeShiThree()
        {
            if (theLeadsLabelOverrule == null)
            {
                theLeadsLabelOverrule = new LeadsLabelRule();
                theLeadsLabelOverrule.TextString = "文章你好";
                Overrule.AddOverrule(RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)), theLeadsLabelOverrule, true);
                theLeadsLabelOverrule.SetCustomFilter();
            }
            Overrule.Overruling = true;
            Application.DocumentManager.MdiActiveDocument.Editor.Regen();
        }
        [CommandMethod("CeShiFour")]
        public void CeShiFour()
        {
            KeyboardHook k_hook = new KeyboardHook();
            k_hook.KeyDownEvent += new System.Windows.Forms.KeyEventHandler(hook_KeyDown);//钩住键按下 
            k_hook.Start();//安装键盘钩子
        }
        private void hook_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            //判断按下的键（Alt + A）
            //if (e.KeyValue == (int)System.Windows.Forms.Keys.A && (int)System.Windows.Forms.Control.ModifierKeys == (int)System.Windows.Forms.Keys.Alt)
            if (e.KeyValue == (int)System.Windows.Forms.Keys.Escape)
            {
                Application.ShowAlertDialog("按下了指定快捷键组合");
            }
        }
    }
}
